package com.projects.xenia.ref_classes;

import android.database.Cursor;
import com.projects.xenia.global_modules.MainGlobalModule;

public class CustomersRefObject {

	public CustomersRefItem RefItem;	
	
	public CustomersRefObject () {	
	
		RefItem = new CustomersRefItem ();
		
	}	

	public CustomersRefObject (String customer_id) {
		
		if (customer_id!=null && !GetCustomerById (customer_id)) {
			RefItem = new CustomersRefItem ();
			RefItem.id = customer_id;
			RefItem.descr = "<object not found>";
		}
		
	}
	
	public boolean GetCustomerById (String customer_id) {
		
		boolean _result = false;
		
		Cursor CustomerCursor = MainGlobalModule.DBE.rawQuery("select id,descr,adress,telephone,email,outletdescr,outletadress,outlettelephone,opentime,closetime,breacktimefrom,breacktimeto from xref_Customers where id = ?", new String [] {customer_id});
		if (CustomerCursor.getCount()>0) {
			CustomerCursor.moveToFirst();
		
			RefItem = new CustomersRefItem ();
			
			RefItem.id =  CustomerCursor.getString(0);
			RefItem.descr =  CustomerCursor.getString(1); 
			RefItem.adress =  CustomerCursor.getString(2); 
			RefItem.telephone =  CustomerCursor.getString(3); 
			RefItem.email =  CustomerCursor.getString(4); 
			RefItem.outletdescr =  CustomerCursor.getString(5); 
			RefItem.outletadress =  CustomerCursor.getString(6); 
			RefItem.outlettelephone =  CustomerCursor.getString(7); 
			RefItem.opentime =  CustomerCursor.getString(8); 
			RefItem.closetime =  CustomerCursor.getString(9); 
			RefItem.breacktimefrom =  CustomerCursor.getString(10); 
			RefItem.breacktimeto =  CustomerCursor.getString(11);	

			Cursor CustomerGPSCursor = MainGlobalModule.DBE.rawQuery("select lat, lon from xref_CustomersGps where customers_id = ?", new String [] {RefItem.id});
			
			if (CustomerGPSCursor.getCount()>0) {
				CustomerGPSCursor.moveToFirst();		
				RefItem.lat = Double.parseDouble(CustomerGPSCursor.getString(0));
				RefItem.lon = Double.parseDouble(CustomerGPSCursor.getString(1));
			}
			
			CustomerGPSCursor.close();

			_result = true;
			
		}
		
		CustomerCursor.close();
		
		return (_result);
		
	}
	
	public String GetCustomersDescrLong () {
		
		String _result = "";
		
		if (RefItem!=null) {
			_result = String.format("%s,%s", RefItem.descr,RefItem.outletdescr);
		}
		
		return (_result);
		
	}
	
}
